<?php
namespace App\Console\Commands;
use App\Models\Student;
use Illuminate\Console\Command;

class UpdateStudentInitials extends Command
{
    protected $signature = 'student:update-initials';
    protected $description = '批量更新学生姓名的首字母大写';

    public function handle()
    {
        // 批量获取所有学生，分批处理（避免内存溢出）
        Student::query()
            ->whereNull('upper_initial')->orWhereNull('first_word')
            ->chunkById(100, function ($students) {
                foreach ($students as $student) {
                    $student->upper_initial = Student::getChineseInitial($student->name);
                    $student->first_word = Student::getFirstCharacterPinyin($student->name);
                    $student->save();
                }
                $this->info('已更新 ' . count($students) . ' 条学生记录');
            });

        $this->info('所有学生首字母更新完成！');
    }
}